﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using PairingMatrix.Common;
using PairingMatrix.Common.Enum;
using PairingMatrix.Services;

namespace PairingMatrix.Model
{
    public class ProjectCardModel : BusinessModelBase
    {
        private readonly PairingMatrixServiceClient _dataService;
        public ObservableCollection<ProjectCardModel> SecondaryProjectCards { get; set; }

        public ProjectCardModel(PairingMatrixServiceClient dataService, ProjectCard card, WallType wall)
        {
            _dataService = dataService;
            _cardNumber = card.CardNumber;
            _title = card.Title;
            _note = "";
            _devStage = (StoryStatus)Enum.Parse(typeof(StoryStatus), card.StoryStatus.Replace(" ", "").Replace("-", ""), true);
            _position = new Point();
            _isMingleCard = card.IsMingleCard;
            _statusInMingle = card.IsMingleCard ? card.StoryStatus : "";
            _numberOfSignUps = card.TotalNumberOfSignUps;
            DevEstimate = card.DevEstimate;
            _wall = wall;
            SecondaryProjectCards = new ObservableCollection<ProjectCardModel>();
            _priority = card.Priority;
            Registry.Settings.PropertyChanged += Settings_PropertyChanged;
        }

        void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "ShowDaysSpentForProjectCards")
            {
                OnPropertyChanged("ShowDaysSpentForProjectCards");
            }
        }
        
        private int _cardNumber;
        public int CardNumber
        {
            get { return _cardNumber; }
            set
            {
                if (_cardNumber == value)
                    return;
                _cardNumber = value;
                OnPropertyChanged("CardNumber");
            }
        }

        private int _priority;
        public int Priority
        {
            get { return _priority; }
            set
            {
                if (_priority == value)
                    return;
                _priority = value;
                OnPropertyChanged("Priority");
            }
        }

        private readonly WallType _wall;
        public WallType Wall
        {
            get { return _wall; }
        }

        private string _statusInMingle;
        public string StatusInMingle
        {
            get { return _statusInMingle; } 
            set
            {
                if (_statusInMingle == value)
                    return;
                _statusInMingle = value;
                OnPropertyChanged("StatusInMingle");
            }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title == value)
                    return;
                _title = value;
                OnPropertyChanged("Title");
            }
        }

        private string _note;
        public string Note
        {
            get { return _note; }
            set
            {
                if (_note == value)
                    return;
                _note = value;
                if (!Registry.LoadingWallForFirstTime)
                    _dataService.UpdateNoteForStorySignUpAsync(_cardNumber, Registry.CurrentDate, _note);
                OnPropertyChanged("Note");
            }
        }

        public bool shallSetProjectOnWall = false;
        private Point _position;
        public Point Position
        {
            get { return _position; }
            set
            {
                if (_position == value)
                    return;
                _position = value;
                if (!Registry.LoadingWallForFirstTime)
                    _dataService.AddProjectCardOnWallInstanceForDateAsync(Registry.CurrentDate, _cardNumber, _position.X, _position.Y, DevStage.ToString(), Wall.ToString(), _title);
                OnPropertyChanged("Position");
            }
        }

        private StoryStatus _devStage;
        public StoryStatus DevStage
        {
            get { return _devStage; }
            set
            {
                if (_devStage == value)
                    return;
                _devStage = value;
                OnPropertyChanged("DevStage");
            }
        }

        public bool IsCardVisibleInCurrentWall 
        {
            get { return _wall == Registry.CurrentWall; }
        }



        public void Save()
        {
            _dataService.UpdateProjectCardDetailsAsync(_cardNumber, _title);
        }


        private TeamMemberModel _assignedTeamMember1;
        public TeamMemberModel AssignedTeamMember1
        {
            get { return _assignedTeamMember1; }
            set
            {
                if (_assignedTeamMember1 == value)
                    return;
                _assignedTeamMember1 = value;
                OnPropertyChanged("AssignedTeamMember1");
            }
        }

        private TeamMemberModel _assignedTeamMember2;
        public TeamMemberModel AssignedTeamMember2
        {
            get { return _assignedTeamMember2; }
            set
            {
                if (_assignedTeamMember2 == value)
                    return;
                _assignedTeamMember2 = value;
                OnPropertyChanged("AssignedTeamMember2");
            }
        }

        private bool _isSyncing; 
        public bool IsSyncing 
        {
            get { return _isSyncing; }
            set
            {
                if (_isSyncing == value)
                    return;
                _isSyncing = value;
                OnPropertyChanged("IsSyncing");
            }
        }

        private bool _isMingleCard; 
        public bool IsMingleCard
        {
            get { return _isMingleCard; }
            set
            {
                if (_isMingleCard == value)
                    return;
                _isMingleCard = value;
                OnPropertyChanged("IsMingleCard");
            }
        }

        private int _numberOfSignUps;
        public int NumberOfSignUps 
        {
            get { return _numberOfSignUps; }
            set
            {
                if (_numberOfSignUps == value)
                    return;
                _numberOfSignUps = value;
                OnPropertyChanged("NumberOfSignUps");
            }
        }

        private double _devEstimate;
        public double DevEstimate 
        {
            get { return _devEstimate; }
            set
            {
                if (_devEstimate == value)
                    return;
                _devEstimate = value;
                CalculateUrgency();
                OnPropertyChanged("DevEstimate");
            }
        }

        private void CalculateUrgency()
        {
            var ratioOfSignUpToPoints = NumberOfSignUps / DevEstimate;
            if (ratioOfSignUpToPoints <= Registry.Settings.Point1MappingToNotUrgent)
                Urgency = Urgency.NotUrgent;
            else if (ratioOfSignUpToPoints <= Registry.Settings.Point1MappingToUrgent)
                Urgency = Urgency.Urgent;
            else
                Urgency = Urgency.VeryUrgent;
        }


        private Urgency _urgency;
        public Urgency Urgency
        {
            get { return _urgency; }
            set
            {
                if (_urgency == value)
                    return;
                _urgency = value;
                OnPropertyChanged("Urgency");
            }
        }


        public bool SignUpTeamMemberForThisCard(TeamMemberModel teamMember)
        {
            if (AssignedTeamMember1 == null)
            {
                AssignedTeamMember1 = teamMember;
                return true;
            }

            if (AssignedTeamMember2 == null)
            {
                AssignedTeamMember2 = teamMember;
                return true;
            }

            return false;
        }

        public void SignUpSecondaryProjectCard(ProjectCardModel secondaryCard)
        {
            secondaryCard.PrimaryCard = this;
            SecondaryProjectCards.Add(secondaryCard);
        }

        private ProjectCardModel _primaryCard;
        public ProjectCardModel PrimaryCard
        {
            get { return _primaryCard; }
            set
            {
                if (_primaryCard == value)
                    return;
                _primaryCard = value;
                OnPropertyChanged("PrimaryCard");
            }
        }

        public bool ShowDaysSpentForProjectCards
        {
            get { return Registry.Settings.ShowDaysSpentForProjectCards; }
            set
            {
                if (Registry.Settings.ShowDaysSpentForProjectCards == value)
                    return;
                Registry.Settings.ShowDaysSpentForProjectCards = value;
                OnPropertyChanged("ShowDaysSpentForProjectCards");
            }
        }

   }
}